// Declare the TaskManager Data Access namespace.
TaskManager.DataAccess = {}

TaskManager.DataAccess._sprints = null;
TaskManager.DataAccess._sprintTasks = null;
TaskManager.DataAccess._developers = null;


// SPRINTS

TaskManager.DataAccess.getSprints = function(callback) {
	try {
		if (TaskManager.DataAccess._sprints == null) {
			$.ajax({
				url: TaskManager._server + "/Sprints",
				dataType: "jsonp",
				jsonp: "d",
				cache: false
			})
			.done(function (response) {
				TaskManager.DataAccess._sprints = response.sprints;
				callback(TaskManager.DataAccess._sprints);
			})
		} else {
			callback(TaskManager.DataAccess._sprints);
		}
	} catch (err) {
	}
}

TaskManager.DataAccess.getSprint = function(id) {
	try {
		return JSON.parse(JSON.stringify(JSON.find({ data: TaskManager.DataAccess._sprints, criteria: [{ elementName: "SprintId", elementValue: id }] })));
	} catch (err) {
	}
}

TaskManager.DataAccess.saveSprint = function(sprint, callback) {
	try {
		$.ajax({
			url: TaskManager._server + "/SaveSprint",
			data: { data: JSON.stringify(sprint) },
			dataType: "jsonp",
			jsonp: "d",
			cache: false
		})
		.done(function (response) {
			// If there was an error...
			if (response.result.length > 0) {
				var sprint = JSON.find({ data: TaskManager.DataAccess._sprints, criteria: [{ elementName: "SprintId", elementValue: response.sprint.Id }] });
				sprint = response.sprint;
			}
			callback(response);
		})
	} catch (err) {
	}
}


// TASKS

TaskManager.DataAccess.getSprintTasks = function(id, callback) {
	if (TaskManager.DataAccess._sprintTasks != null && TaskManager.DataAccess._sprintTasks.id == id) {
		callback(TaskManager.DataAccess._sprintTasks.items);
	} else {
		try {
			$.ajax({
				url: TaskManager._server + "/SprintTasks",
				data: { id: id },
				dataType: "jsonp",
				jsonp: "d",
				cache: false
			})
			.done(function (response) {
				TaskManager.DataAccess._sprintTasks = { id: id, items: response.tasks };
				callback(response.tasks);
			});
		} catch (err) {
		}
	}
}

TaskManager.DataAccess.getTask = function(id) {
	try {
		return JSON.parse(JSON.stringify(JSON.find({ data: TaskManager.DataAccess._sprintTasks.items, criteria: [{ elementName: "TaskId", elementValue: id }] })));
	} catch (err) {
	}
}

TaskManager.DataAccess.saveTask = function(task, callback) {
	$.ajax({
		url: TaskManager._server + "/SaveTask",
		data: { data: JSON.stringify(task) },
		dataType: "jsonp",
		jsonp: "d",
		cache: false
	})
	.done(function (response) {
		JSON.replaceArrayElement(TaskManager.DataAccess._sprintTasks.items, "Id", task.Id, task);
		callback(response);
	})
}


// DEVELOPERS

TaskManager.DataAccess.getDevelopers = function(callback) {
	try {
		if (TaskManager.DataAccess._developers == null) {
			$.ajax({
				url: TaskManager._server + "/Developers",
				dataType: "jsonp",
				jsonp: "d",
				cache: false
			})
			.done(function (response) {
				TaskManager.DataAccess._developers = response.developers;
				callback(TaskManager.DataAccess._developers);
			})
		} else {
			callback(TaskManager.DataAccess._developers);
		}
	} catch (err) {
	}
}

TaskManager.DataAccess.getDeveloper = function(id) {
	try {
		return JSON.parse(JSON.stringify(JSON.find({ data: TaskManager.DataAccess._developers, criteria: [{ elementName: "DeveloperId", elementValue: id }] })));
	} catch (err) {
	}
}

TaskManager.DataAccess.saveDeveloper = function(developer, callback) {
	$.ajax({
		url: TaskManager._server + "/SaveDeveloper",
		data: { data: JSON.stringify(developer) },
		dataType: "jsonp",
		jsonp: "d",
		cache: false
	})
	.done(function (response) {
		JSON.replaceArrayElement(TaskManager.DataAccess._developers, "DeveloperId", developer.DeveloperId, developer);
		callback(response);
	})
}